package com.test.ch10;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/13
 * @Description:
 */
import java.util.Random;

public class TurtleRabbitRace {
    private static final int TOTAL_DISTANCE = 100;
    private static int turtlePosition = 0;
    private static int rabbitPosition = 0;
    private static String winner = null;
    private static final Object lock = new Object();

    public static void main(String[] args) {
        System.out.println("🐢🐇 龟兔赛跑开始！ 🐇🐢");
        System.out.println("比赛距离：" + TOTAL_DISTANCE + "米");
        System.out.println("=================================");

        // 创建并启动乌龟线程
        Thread turtleThread = new Thread(new Turtle());
        // 创建并启动兔子线程
        Thread rabbitThread = new Thread(new Rabbit());

        turtleThread.start();
        rabbitThread.start();

        // 等待两个线程结束
        try {
            turtleThread.join();
            rabbitThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("=================================");
        System.out.println("🏁 比赛结束！冠军是：" + winner + " 🏁");
    }

    // 乌龟跑步任务
    static class Turtle implements Runnable {
        private Random random = new Random();

        @Override
        public void run() {
            while (turtlePosition < TOTAL_DISTANCE && winner == null) {
                // 生成随机数决定谁跑
                double chance = random.nextDouble();

                synchronized(lock) {
                    if (chance >= 0.3 && chance <= 1.0) {
                        // 乌龟跑
                        turtlePosition += 1;
                        System.out.println("🐢 乌龟前进 1 米，当前位置：" + turtlePosition + "米");

                        // 检查是否获胜
                        if (turtlePosition >= TOTAL_DISTANCE && winner == null) {
                            winner = "乌龟";
                            System.out.println("🎉 乌龟冲过终点！乌龟获得冠军！");
                        }
                    }
                }

                // 短暂休眠，模拟跑步时间
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 兔子跑步任务
    static class Rabbit implements Runnable {
        private Random random = new Random();

        @Override
        public void run() {
            while (rabbitPosition < TOTAL_DISTANCE && winner == null) {
                // 生成随机数决定谁跑
                double chance = random.nextDouble();

                synchronized(lock) {
                    if (chance >= 0.0 && chance < 0.3) {
                        // 兔子跑
                        rabbitPosition += 2;
                        System.out.println("🐇 兔子前进 2 米，当前位置：" + rabbitPosition + "米");

                        // 检查是否获胜
                        if (rabbitPosition >= TOTAL_DISTANCE && winner == null) {
                            winner = "兔子";
                            System.out.println("🎉 兔子冲过终点！兔子获得冠军！");
                        }
                    }
                }

                // 短暂休眠，模拟跑步时间
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}